package com.letoken.platform.admin.controller;

import com.letoken.platform.admin.service.AdminMenuService;
import com.letoken.platform.pub.annotation.auth.Permission;
import com.letoken.platform.pub.annotation.pub.Trim;
import com.letoken.platform.pub.req.PageData;
import com.letoken.platform.pub.res.ApiResponse;
import com.letoken.platform.repo.admin.req.menu.*;
import com.letoken.platform.repo.admin.res.menu.*;
import com.letoken.platform.repo.admin.res.system.QueryAdminMenuTreeRes;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 谢辉
 * @Description admin权限相关接口
 * @date 2023/6/21 17:55
 * @Version 1.0
 */
@RestController
@RequestMapping("/permission")
@Api(tags = "admin权限相关接口")
public class AdminPermissionController {

    @Autowired
    private AdminMenuService adminMenuService;

    @Permission(uri = "/permission/admin/menu" ,qx = "/admin/menu")
    @Trim
    @PostMapping("/query")
    @ApiOperation(value = "查询菜单列表", notes = "查询菜单列表")
    public ApiResponse<PageData<QueryAdminMenuRes>> queryAdminMenuList(@RequestBody @Validated QueryAdminMenuReq req) {
        return ApiResponse.create(adminMenuService.queryAdminMenuList(req));
    }

    @Permission(uri = "/permission/admin/menu" ,qx = "/menu/query")
    @Trim
    @PostMapping("/query/{adminMenuId}")
    @ApiOperation(value = "根据id查询菜单", notes = "根据id查询菜单")
    public ApiResponse<QueryAdminMenuByIdRes> queryAdminMenuById(@PathVariable("adminMenuId") Integer adminMenuId) {
        return ApiResponse.create(adminMenuService.queryAdminMenuById(adminMenuId));
    }

    @Permission(uri = "/menu/add",qx = "/menu/add")
    @Trim
    @PostMapping("/add")
    @ApiOperation(value = "新增菜单", notes = "新增菜单")
    public ApiResponse addAdminMenu(@RequestBody @Validated AddAdminMenuReq req) {
        adminMenuService.addAdminMenu(req);
        return ApiResponse.create("新增成功！");
    }

    @Permission(uri = "/menu/update" ,qx = "/menu/update")
    @Trim
    @PostMapping("/update")
    @ApiOperation(value = "修改菜单", notes = "修改菜单")
    public ApiResponse updateAdminMenu(@RequestBody @Validated UpdateAdminMenuReq req) {
        adminMenuService.updateAdminMenu(req);
        return ApiResponse.create();
    }

    @Permission(uri = "/menu/delete",qx = "/menu/delete")
    @Trim
    @PostMapping("/delete/{adminMenuId}")
    @ApiOperation(value = "删除菜单", notes = "删除菜单")
    public ApiResponse deleteAdminMenu(@PathVariable("adminMenuId") Integer adminMenuId) {
        adminMenuService.deleteAdminMenu(adminMenuId);
        return ApiResponse.create();
    }

    @Trim
    @PostMapping("/page/query")
    @ApiOperation(value = "查询页面列表", notes = "查询页面列表")
    @Permission(uri = "/permission/page/query",qx = "/admin/menu")
    public ApiResponse<PageData<QueryPageRes>> queryPageList(@RequestBody @Validated QueryPageReq req) {
        return ApiResponse.create(adminMenuService.queryPageList(req));
    }

    @Permission(uri = "/permission/page/queryPageById",qx="/menu/query")
    @Trim
    @PostMapping("/page/queryPageById/{adminMenuId}")
    @ApiOperation(value = "根据id查询页面", notes = "根据id查询页面")
    public ApiResponse<QueryPageByIdRes> queryPageById(@PathVariable("adminMenuId") Integer adminMenuId) {
        return ApiResponse.create(adminMenuService.queryPageById(adminMenuId));
    }

    @Permission(uri = "/permission/page/addPage",qx = "/menu/add")
    @Trim
    @PostMapping("/page/addPage")
    @ApiOperation(value = "新增页面", notes = "新增页面")
    public ApiResponse addPage(@RequestBody @Validated AddPageReq req) {
        adminMenuService.addPage(req);
        return ApiResponse.create("新增资源成功！");
    }

    @Permission(uri = "/permission/page/updatePage",qx = "/menu/update")
    @Trim
    @PostMapping("/page/updatePage")
    @ApiOperation(value = "修改页面", notes = "修改页面")
    public ApiResponse updatePage(@RequestBody @Validated UpdatePageReq req) {
        adminMenuService.updatePage(req);
        return ApiResponse.create("修改页面成功！");
    }

    @Permission(uri = "/permission/page/delete" ,qx = "/menu/delete")
    @Trim
    @PostMapping("/page/delete/{adminMenuId}")
    @ApiOperation(value = "删除页面", notes = "删除页面")
    public ApiResponse deletePageById(@PathVariable("adminMenuId") Integer adminMenuId) {
        adminMenuService.deleteResourcesById(adminMenuId);
        return ApiResponse.create("删除页面成功！");
    }
    @Permission(uri = "/permission/resources/query" ,qx="/admin/resource")
    @Trim
    @PostMapping("/resources/query")
    @ApiOperation(value = "资源列表", notes = "查询资源列表")
    public ApiResponse<PageData<QueryResourcesRes>> queryResourcesList(@RequestBody @Validated QueryResourcesReq req) {
        return ApiResponse.create(adminMenuService.queryResourcesList(req));
    }

    @Permission(uri = "/permission/resources/query",qx="/admin/resource")
    @Trim
    @PostMapping("/resources/query/{adminMenuId}")
    @ApiOperation(value = "根据id查询资源", notes = "根据id查询资源")
    public ApiResponse<QueryResourcesByIdRes> queryResourcesById(@PathVariable("adminMenuId") Integer adminMenuId) {
        return ApiResponse.create(adminMenuService.queryResourcesById(adminMenuId));
    }

    @Permission(uri = "/permission/resources/add",qx = "/permission/resources/add")
    @Trim
    @PostMapping("/resources/add")
    @ApiOperation(value = "新增资源", notes = "新增资源")
    public ApiResponse addResources(@RequestBody @Validated AddResourcesReq req) {
        adminMenuService.addResources(req);
        return ApiResponse.create("新增资源成功！");
    }

    @Permission(uri = "/permission/resources/update",qx = "/permission/resources/update")
    @Trim
    @PostMapping("/resources/update")
    @ApiOperation(value = "修改资源", notes = "修改资源")
    public ApiResponse updateResources(@RequestBody @Validated UpdateResourcesReq req) {
        adminMenuService.updateResources(req);
        return ApiResponse.create("修改资源成功！");
    }

    @Permission(uri = "/permission/resources/delete",qx = "/permission/resources/delete")
    @Trim
    @PostMapping("/resources/delete/{adminMenuId}")
    @ApiOperation(value = "删除资源", notes = "删除资源")
    public ApiResponse deleteResourcesById(@PathVariable("adminMenuId") Integer adminMenuId) {
        adminMenuService.deleteResourcesById(adminMenuId);
        return ApiResponse.create("删除资源成功！");
    }


    @PostMapping("/query/onelevel")
    @ApiOperation(value = "查询一级菜单列表", notes = "查询一级菜单列表")
    public ApiResponse<List<QueryOneLevelRes>> queryOneLevelList() {
        return ApiResponse.create(adminMenuService.queryOneLevelList());
    }

    @PostMapping("/query/twolevel")
    @ApiOperation(value = "查询二级菜单列表", notes = "查询二级菜单列表")
    public ApiResponse<List<QueryOneLevelRes>> queryTwoLevelList(@PathVariable("pId") Integer pId) {
        return ApiResponse.create(adminMenuService.queryTwoLevelList(pId));
    }


    @Trim
    @PostMapping("/permission/getPermissionById/{adminRoleId}")
    @ApiOperation(value = "根据角色id查询所有权限", notes = "根据角色id查询所有权限")
    public ApiResponse<List<QueryAdminMenuTreeRes>> getPermissionById(@PathVariable("adminRoleId") Integer adminRoleId) {
        return ApiResponse.create(adminMenuService.getPermissionById(adminRoleId));
    }

    @PostMapping("/permission/getPermission")
    @ApiOperation(value = "查询所有(菜单,页面,按钮)权限", notes = "查询所有(菜单,页面,按钮)权限")
    public ApiResponse<List<QueryAdminMenuTreeRes>> getPermission() {
        return ApiResponse.create(adminMenuService.getPermissionList());
    }

    @PostMapping("/permission/getMePermission")
    @ApiOperation(value = "登录后根据token查询所有权限", notes = "根据token查询所有权限")
    public ApiResponse<List<QueryAdminMenuTreeRes>> getMePermission() {
        return ApiResponse.create(adminMenuService.getMePermission());
    }
    @Permission(uri = "/permission/permission/modifyPermission",qx = "/admin/permissions")
    @Trim
    @PostMapping("/permission/modifyPermission")
    @ApiOperation(value = "修改角色权限", notes = "修改角色权限")
    public ApiResponse modifyPermission(@RequestBody @Validated UpdatePermissionReq req) {
        adminMenuService.modifyPermission(req);
        return ApiResponse.create("设置权限成功！");
    }

    @PostMapping("/permission/allPermission")
    @ApiOperation(value = "给超级管理授权所有（菜单,页面,资源）权限", notes = "给超级管理授权所有（菜单,页面,资源）权限")
    public ApiResponse allPermission() {
        adminMenuService.allPermission();
        return ApiResponse.create("设置权限所有成功！");
    }
}
